package com.jay.lang.management.clazz;

import java.lang.management.LockInfo;
import java.lang.management.ManagementFactory;
import java.lang.management.MonitorInfo;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockInfoCls {
	static volatile boolean flag = true;

	public static void main(String[] args) {

		new Thread() {
			final Lock lock = new ReentrantLock();

			@Override
			public void run() {
				this.setName("Wooooo");
				synchronized (this) {
					while (flag) {
						try {
							lock.lock();
							sleep(1000);
							lock.unlock();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}

		}.start();

		ThreadMXBean mxbean = ManagementFactory.getThreadMXBean();

		ThreadInfo[] tis = mxbean.dumpAllThreads(true, true);
		// long[] ids = mxbean.getAllThreadIds();
		// ThreadInfo[] tis = mxbean.getThreadInfo(ids);
		for (ThreadInfo ti : tis) {
			System.out.println("---------------");
			System.out.println(ti);
			System.out.println(ti.getBlockedCount());
			System.out.println(ti.getBlockedTime());
			System.out.println(ti.getLockName());
			System.out.println(ti.getLockOwnerId());
			System.out.println(ti.getLockOwnerName());
			System.out.println(ti.getThreadId());
			System.out.println(ti.getThreadName());
			System.out.println(ti.getWaitedCount());
			System.out.println(ti.getWaitedTime());
			System.out.println(ti.isInNative());
			System.out.println(ti.isSuspended());
			System.out.println(ti.getThreadState());

			System.out.println("-----Locks-----");
			LockInfo[] lis = ti.getLockedSynchronizers();
			for (LockInfo li : lis) {

				System.out.println("***lock***");
				System.out.println(li);
			}
			System.out.println("-----Monitors-----");
			MonitorInfo[] mis = ti.getLockedMonitors();
			for (MonitorInfo mi : mis) {

				System.out.println("***monitor***");
				System.out.println(mi);
				System.out.println(mi.getLockedStackDepth());
				System.out.println(mi.getLockedStackFrame());
			}
		}
		flag = false;
	}
}
